import { ys, ysUtils } from "ys-ts";
import { useRouter as baseRouter, useRoute } from "vue-router";
import Config from "@/components/pages/Config.vue";
import Animation from "@/components/pages/Animation.vue";

export interface IToNameConfig {
  name: string;
  params?: any;
}

export function useRouter() {
  const router = baseRouter();
  const route = useRoute();

  async function toName(name: string, params?: any) {
    if (router.hasRoute(name)) {
      await router.push({
        name: name,
        params: params,
      });
    } else {
      console.error(`[router] ${name} 不存在`);
    }
  }

  return {
    router,
    route,
    toName,
  };
}

/**
 * 所有的路由名称由此导出
 * 路由必须命名
 * @returns
 */
export function useRouterName() {
  return {
    config: "config",
    animation: "animation",
  };
}

export function useRouterRoute() {
  console.log(`[router] init`);

  const name = useRouterName();

  const routerUtil = ys.getRouterUtil();
  // 补充路由数据
  const cfgConfig = new ysUtils.YsRouteRecord({
    name: name.config,
    path: "",
    component: Config,
  });
  routerUtil.addConfig(cfgConfig.build());

  const cfgAnimation = new ysUtils.YsRouteRecord({
    name: name.animation,
    path: "/animation",
    component: Animation,
  });
  routerUtil.addConfig(cfgAnimation.build());

  // 补充前置函数
}

export function createRouter() {
  useRouterRoute();
  return ys.getRouterUtil().build();
}
